package com.decent.manager.system.dao;

import com.decent.common.annotation.ReturnCheck;
import com.decent.common.entity.manager.SystemUser;
import com.decent.manager.system.auth.vo.LoginUser;
import com.decent.manager.system.dao.provider.SystemUserDaoProvider;
import com.decent.manager.system.dto.SystemUserDTO;
import com.decent.manager.system.vo.SystemUserVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户sql管理
 *
 * @author wangyan
 **/
@Mapper
public interface SystemUserDao {
    /**
     * 根据用户名查询用户
     *
     * @param id 用户id
     * @return 结果
     */
    @Select("select id, user_account, login_password, user_name, mobile, last_login_ip, " +
            " last_login_time, user_type, user_status" +
            " from system_user where id = #{id}")
    SystemUser queryUserById(Long id);

    /**
     * 根据用户名查询用户
     *
     * @param account 用户名
     * @return 结果
     */
    @Select("select id, `user_account`, `login_password`, `user_name`, mobile, last_login_ip, " +
            " last_login_time, user_type, `user_status` " +
            " from system_user where `user_account` = #{account} ")
    SystemUser queryUserByAccount(String account);

    /**
     * 根据手机号查询用户
     *
     * @param mobile 手机号
     * @return 结果
     */
    @Select("select id, `user_account`, `login_password`, `user_name`, mobile, last_login_ip, " +
            " last_login_time, user_type, `user_status`" +
            " from system_user where `mobile` = #{mobile} ")
    SystemUser queryUserByMobile(String mobile);

    /**
     * 根据用户id更新用户
     *
     * @param principal 用户数据
     * @return 结果
     */
    @UpdateProvider(type = SystemUserDaoProvider.class, method = "updateUserById")
    @ReturnCheck(info = "用户更新失败")
    int updateUserById(@Param("user") LoginUser principal);

    /**
     * 分页查询公司员工列表
     *
     * @param user 查询条件
     * @return list
     */
    @SelectProvider(type = SystemUserDaoProvider.class, method = "queryPage")
    List<SystemUserVO> queryPage(@Param("user") SystemUserDTO user);

    /**
     * 判断是否存在重复登录账号或手机号的员工
     *
     * @param user 查询信息
     * @return true：有重复；false：无重复
     */
    @Select("<script>" +
            "select 1 " +
            "from system_user " +
            "where (user_account = #{user.userAccount}  " +
            "<if test =\"user.mobile!=null and user.mobile !='' \">" +
            " or mobile = #{user.mobile} " +
            "</if>" +
            ") " +
            "<if test =\"user.id!=null \">" +
            " and id &lt;&gt; #{user.id} " +
            "</if>" +
            "limit 1" +
            "</script>")
    Boolean queryRepeatUser(@Param("user") SystemUserDTO user);

    /**
     * 创建员工
     *
     * @param user 新增信息
     * @return 新增行数
     */
    @InsertProvider(type = SystemUserDaoProvider.class, method = "createUser")
    @SelectKey(before = false, keyProperty = "user.id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID()")
    int createUser(@Param("user") SystemUserDTO user);

    /**
     * 修改员工
     *
     * @param user 修改信息
     */
    @Update("<script>" +
            "UPDATE system_user " +
            "SET" +
            "<if test =\"u.userName!=null  \">" +
            " user_name=#{u.userName}," +
            "</if>" +
            "<if test =\"u.mobile!=null  \">" +
            "mobile=#{u.mobile} " +
            "</if>" +
            "WHERE user_account=#{u.userAccount}" +
            "</script>")
    void updateUser(@Param("u") SystemUserVO user);

    /**
     * 修改员工
     *
     * @param user 修改信息
     */
    @Update("UPDATE system_user_role " +
            "SET role_id=#{u.roleId} " +
            "WHERE user_id=#{u.id} ")
    void updateUserRole(@Param("u") SystemUserVO user);


    /**
     * 重置密码
     *
     * @param user 用户信息
     */
    @Update("UPDATE system_user " +
            "SET login_password=#{u.loginPassword} " +
            "WHERE user_account=#{u.userAccount}")
    void setPassword(@Param("u") SystemUserVO user);
}
